package webDriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import utils.Browser;
import utils.PropertyLoader;

import java.util.concurrent.TimeUnit;

/**
 * Created by Azvonov on 01.09.2014.
 */
public class WebDriverFactory {

    static WebDriver driver;
    final static Long WAIT_IMPLICITLY_DEFAULT_TIMEOUT
            = Long.valueOf(PropertyLoader.loadProperty("wait.implicitly.default"));

    public static WebDriver getDriver(Browser browser) {

        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("nativeEvents", false);
        if (driver != null){
            return  driver;
        }

        switch (browser.getBrowserType()){
            case FIREFOX:
                driver = new FirefoxDriver(desiredCapabilities);
                break;
            case CHROME:
                driver = new ChromeDriver();
                System.setProperty("webdriver.chrome.driver", "c:\\WebDrivers\\chromedriver.exe");
                break;
        }
        driver.manage().timeouts().implicitlyWait(WAIT_IMPLICITLY_DEFAULT_TIMEOUT
                , TimeUnit.SECONDS);
        driver.manage().window().maximize();
        return driver;
    }
}
